﻿using DocumentFormat.OpenXml.Office2010.ExcelAc;
using SeeSharpTools.JY.GUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu
{
    public partial class FrmActualTrend : Form
    {

        private Timer UpdateTimer;
        public FrmActualTrend()
        {
            InitializeComponent();


            //设置x轴数据类型
            this.chart_ActualTrend.XDataType = StripChartX.XAxisDataType.TimeStamp;
            chart_ActualTrend.TimeStampFormat = "HH:mm:ss";

            //设置图例
            this.chart_ActualTrend.LegendVisible = false;

            //设置数据点
            this.chart_ActualTrend.DisplayPoints = 4000;

            //Y1 范围
            this.chart_ActualTrend.AxisY.Minimum = -10.0f;
            this.chart_ActualTrend.AxisY.Maximum = 50.0f;
            this.chart_ActualTrend.AxisY.AutoScale = false;
            //Y2 范围
            this.chart_ActualTrend.AxisY2.Minimum = 0.0f;
            this.chart_ActualTrend.AxisY2.Maximum = 1.0f;
            this.chart_ActualTrend.AxisY2.AutoScale = false;



            if (CommonMethods.plcDevice != null)
            {
                //获取归档变量的数量
                int total = CommonMethods.plcDevice.StoreVarList.Count;
                //清除曲线
                this.chart_ActualTrend.Series.Clear();
                //设置曲线数量
                this.chart_ActualTrend.SeriesCount = total;


                for (int i = 0; i < total; i++)
                {
                    this.chart_ActualTrend.Series[i].Name = CommonMethods.plcDevice.StoreVarList[i].Comments;

                    //设置曲线不可见
                    this.chart_ActualTrend.Series[i].Visible = false;

                    //设置曲线宽度
                    this.chart_ActualTrend.Series[i].Width = StripChartXSeries.LineWidth.Middle;

                    //设置每条曲线的Y轴 是y1 还是y2
                    if (this.chart_ActualTrend.Series[i].Name.Contains("温度"))
                    {
                        this.chart_ActualTrend.Series[i].YPlotAxis = StripChartXAxis.PlotAxis.Primary;
                    }
                    else
                    {
                        this.chart_ActualTrend.Series[i].YPlotAxis = StripChartXAxis.PlotAxis.Secondary;
                    }
                }

                //设置参数

                MainPanel.Controls.Clear();

                int rowGap = 43;
                int colGap = 180;

                int xStart = 48;
                int yStart = 20;

                for (int i = 0;i < total;i++)
                {
                    CheckBox checkBox = new CheckBox();
                    checkBox.AutoSize = true;
                    checkBox.ForeColor = Color.White;
                    checkBox.Name = "chk_" + i.ToString();
                    checkBox.Text = CommonMethods.plcDevice.StoreVarList[i].Comments;
                    checkBox.Tag = i;

                    checkBox.CheckedChanged += CheckBox_CheckedChanged;

                    //设置 Location
                    int rowIndex = i / 6;
                    int colIndex = i % 6;

                    checkBox.Location = new Point(xStart + colIndex * colGap,
                        yStart + rowIndex * rowGap);

                    //设置checked
                    checkBox.Checked = i < CommonMethods.sysConfig.ShowSeriesCount;

                    //添加到panel
                    MainPanel.Controls.Add(checkBox);
                }
            }


            UpdateTimer = new Timer();
            UpdateTimer.Tick += UpdateTimer_Tick;
            UpdateTimer.Interval = 200;
            UpdateTimer.Start();

            this.FormClosing += (s, e) =>
            {
                UpdateTimer.Stop();
            };

        }

        //
        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is CheckBox checkBox)
            {
                if (checkBox.Tag != null && checkBox.Tag.ToString().Length > 0)
                {
                    int index = Convert.ToInt32(checkBox.Tag.ToString());

                    this.chart_ActualTrend.Series[index].Visible = checkBox.Checked;
                }
            }
        }

        //定时更新
        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            if (CommonMethods.plcDevice.IsConnected)
            {
                List<double> ydata = new List<double>();

                foreach (var item in CommonMethods.plcDevice.StoreVarList)
                {
                    string varValue = item.VarValue?.ToString();
                    if (double.TryParse(varValue,out double Value))
                    {
                        ydata.Add(Value);
                    }
                    else
                    {
                        ydata.Add(0.0);
                    }
                }
                this.chart_ActualTrend.PlotSingle(ydata.ToArray());
            }
        }

    }
}
